package Units;

import static org.lwjgl.util.glu.GLU.gluLookAt;

import java.util.ArrayList;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key;

import Game.GameModel;
import Game.scene.Camera;
import Game.scene.Stage;

public class Tank extends GameModel{

	private float vel = 0;
	private float ang = 90;
	private float acel = 8;
	private float maxSpeed = 25;
	public ArrayList<CannonBall> bulletList = new ArrayList<CannonBall>();
	private long cooldown;

	public Tank(float x, float y, float z, String modelPath, String texturePath) {
		super(x, y, z, modelPath, texturePath);
		
		addChild(new Camera(x, y, z));
		parentName = "Player";
	}
	
	@Override
	public void Update(long diftime) {
		if(this.rotateY >= 360){
			this.rotateY = this.rotateY - 360; 
		}
		
		if(this.rotateY < 0){
			this.rotateY = 360 + this.rotateY;
		}
		//move tank
		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
			
			this.rotateY += diftime * 60 / 1000.0f;
			ang += 10.0f * diftime / 1000.0f;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
			
			this.rotateY -= diftime * 60 / 1000.0f;
			ang -= 10.0f *diftime / 1000.0f;
			
		} 
		
		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
			if(vel < maxSpeed)
				vel += diftime * acel / 1000.0f;
		} else {
			
			//perda de velociadade
			if(vel > 0){
				vel -= 3.f * diftime / 1000.f;
				if(vel < 0.1)
					vel = 0;
			}
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
			
			if(vel > maxSpeed*-1+10)
				vel -= diftime * acel / 1000.0f;
			
		} else {
			
			//perda de velociadade
			if(vel < 0){
				vel += 3.f * diftime / 1000.f;
				if(vel > -0.1)
					vel = 0;
			}
		}
		
		//rotate turret
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			modelList.get(0).rotateY-= diftime * 60 / 1000.0f;
			modelList.get(1).rotateY-= diftime * 60 / 1000.0f;
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			modelList.get(0).rotateY+= diftime * 60 / 1000.0f;
			modelList.get(1).rotateY+= diftime * 60 / 1000.0f;
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
			if(System.currentTimeMillis() - cooldown > 1000){
				
				Stage.addChild(Factory.newBullet(x, y+4, z, rotateY+modelList.get(0).rotateY, "Player"));
				cooldown = System.currentTimeMillis();
			}
		}
		
//		if (Mouse.isButtonDown(0)){
//			if(System.currentTimeMillis() - cooldown > 1000){
//				Stage.addChild(Factory.newBullet(x, y+4, z, rotateY));
//				cooldown = System.currentTimeMillis();
//			}
//		}
		
//		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
//			modelList.get(1).rotateZ-= diftime * 60 / 1000.0f;
//		}
		
//		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
//			modelList.get(1).rotateZ+= diftime * 60 / 1000.0f;
//		}
		
		//movimenta peronagens	
		double radians = (Math.PI/180)*this.rotateY;
		x += ((float)Math.cos((double)radians) * vel) * diftime / 1000;
		z += (((float)Math.sin((double)radians) * vel) * diftime / 1000)*-1;
		
//		System.out.println("Angle: "+ang+" rotateY: "+this.rotateY+" velocidade: "+ vel);
	}
	
	@Override
	public void Render() {
		
		super.Render();
	}
}
